package org.pvs.sp.mobile;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import org.pvs.palitos.IA;
import org.pvs.palitos.Jugador;
import org.pvs.palitos.Partida;

public class MobilePalitos extends MIDlet {
	private List mainForm, levelForm;
	private Form gameForm, helpForm;
	private Display display;
	
	public MobilePalitos() {
		display = Display.getDisplay(this);
		createForms();
	}

	private void createForms() {
		// Main form
		mainForm = new List("Super Palitos", ChoiceGroup.IMPLICIT, new String[] { "Jugar",
				"Instrucciones", "Salir" }, null);

//		mainForm.addCommand(new Command("Seleccionar", Command.SCREEN, 0));
		mainForm.addCommand(new Command("Salir", Command.EXIT, 0));
		mainForm.setCommandListener(new CommandListener() {
			public void commandAction(Command cmd, Displayable d) {
				if(cmd.getCommandType() == Command.EXIT) {
					notifyDestroyed();
				} else if(cmd == List.SELECT_COMMAND /*|| cmd.getLabel() == "Seleccionar"*/) {
					switch (mainForm.getSelectedIndex()) {
					case 0:
						display.setCurrent(levelForm);
						break;
					case 1:
						display.setCurrent(helpForm);
						break;
					case 2:
						notifyDestroyed();
						break;
					}
					
				}
			}
		});
		
		// Help form
		helpForm = new Form("Instrucciones");
		helpForm.append(new StringItem(null, "Se juega así..."));
		helpForm.addCommand(new Command("Volver", Command.BACK, 0));
		helpForm.setCommandListener(new CommandListener() {
			public void commandAction(Command cmd, Displayable d) {
				display.setCurrent(mainForm);
			}
		});
		
		// Level form
		levelForm = new List("Dificultad", ChoiceGroup.IMPLICIT, new String[] { "Facil",
				"Normal", "Dificil" }, null);
//		levelForm.addCommand(new Command("Seleccionar", Command.SCREEN, 0));
		levelForm.addCommand(new Command("Volver", Command.BACK, 0));
		levelForm.setCommandListener(new CommandListener() {
			public void commandAction(Command cmd, Displayable d) {
				if(cmd.getCommandType() == Command.BACK) {
					display.setCurrent(mainForm);
				} else {
					int level = levelForm.getSelectedIndex();
					startGame(level);
				}
			}
		});
	}

	protected void startGame(int level) {
		Jugador j2 = new IA(level, Partida.RED_COLOR);
		// do tablero
		Partida p = new Partida(null, j2, null);
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {
		Display display = Display.getDisplay(this);
		/*String res = "Res: " + mMainForm.getWidth() + "x" + mMainForm.getHeight();
		String colors = "Colors: " + display.numColors() + "x" + display.numAlphaLevels();
		mMainForm.append(new StringItem(null, res));
		mMainForm.append(new StringItem(null, colors));*/
		display.setCurrent(mainForm);
	}
}
